<?php
include_once('libs/class.database.php');

class Berita
{
	public $link;
	
	function __construct()
	{
		$konek= new Database();
		$this->link=$konek->connect();
	}
	
	function tambahBerita($judul, $isi_berita, $gambar ,$id_username)
	{
		$tgl_berita=date('Y-m-d | H:i:s');
		$query=$this->link->prepare("INSERT INTO berita(judul_berita, isi_berita, gambar, id_username, tgl_berita) 
			 VALUES(?,?,?,?,?)");
		$data=array($judul, $isi_berita, $gambar, $id_username, $tgl_berita);
		$query->execute($data);
		$jml_row=$query->rowCount();
		return $jml_row;
	}
	function editBerita($judul, $isi_berita, $gambar ,$id_username)
	{
		$tgl_berita=date('Y-m-d | H:i:s');
			 $query=$this->link->query("UPDATE berita SET judul_berita='$judul_berita', isi_berita='$isi_berita', gambar='$gambar', id_username='$id_username', tgl_berita='$tgl_berita' WHERE no_berita='$no_berita'");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
	 function getAllBerita()
	{
		$query=$this->link->query("SELECT * FROM berita ORDER BY tgl_berita ASC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getAllPageBerita($hal)
	{
		$next=$hal * 3;
		$top= 3;
		if($next==0)
		{
			$query=$this->link->query("SELECT * FROM berita  ORDER BY tgl_berita ASC LIMIT $top");
		}
		else
		{
			$query=$this->link->query("SELECT * FROM berita  ORDER BY tgl_berita ASC LIMIT $next,$top");
		}
		
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getBerita($no_berita)
	{
		$query=$this->link->query("SELECT * FROM berita WHERE no_berita='$no_berita' ORDER BY tgl_berita DESC");
		$jml_data=$query->rowCount();
		if($jml_data==1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function getBeritaByUsername($id_username)
	{
		$query=$this->link->query("SELECT * FROM berita WHERE id_username='$id_username' ORDER BY tgl_berita DESC");
		$jml_data=$query->rowCount();
		if($jml_data>=1)
		{
			$hasil=$query->fetchAll();
			return $hasil;
		}
		else
		{
			return $jml_data;
		}
	}
	
	function updateBerita($no_berita, $judul, $isi_berita)
	{
		$tgl_berita=date('Y-m-d');
		$query=$this->link->query("UPDATE berita SET judul='$judul', isi_berita='$isi_berita' WHERE no_berita='$no_berita'");
		$hasil=$query->rowCount();
		return $hasil;
	}
	
	function deleteBerita($no_berita)
	{
		$query=$this->link->query("Delete berita WHERE no_berita='$no_berita'");
		$hasil=$query->rowCount();
		return $hasil;
	}
}
?>